
package com.birdcage.goldminer.ui;

import java.util.Vector;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.birdcage.gamemanagers.GraphicsManager;

///Singleton
///This class will manage all the UI logic.
public class UIManager 
{   
    ///Private member
    private UIComponent m_rootComponent = new UIComponent("Root", EUIComponetType.eUICT_Root);
    private boolean m_active = true;            // if answer any mouse operation
    private Vector<UIComponent> m_modalComp;
    
    ///Singleton Implement
    static public UIManager GetInstance()
    {
        if (m_instance == null)
        {
            m_instance = new UIManager();
        }       
        return m_instance;
    }
    private UIManager(){};
    private static UIManager m_instance = null;
    
    ///Public Method.
    public UIComponent GetRoot()
    {
        return m_rootComponent;
    }
    
    public void SetActive(boolean m_active)
    {
        this.m_active = m_active;
    }
    public boolean GetActive()
    {
        return m_active;
    }
    public void SetModalComp(UIComponent modalComp)
    {
        this.m_modalComp.add(modalComp);
    }
    public Vector<UIComponent> GetModalComp()
    {
        return m_modalComp;
    }
    
    // we do two things in update.
    //1. answer operation
    //2. render
    public void Update()
    {
        //answer operation
        boolean isTouched = Gdx.input.isTouched();
        boolean isModalState = m_modalComp.size() != 0;
        if (isTouched)
        {
            Vector2 touchPos = new Vector2(Gdx.input.getX(), Gdx.input.getY());
            if (isModalState)
            {
                UIComponent topModalComp = m_modalComp.lastElement();
                if (topModalComp.HandleTouch(touchPos))
                {
                    m_modalComp.remove(topModalComp);
                }
            }
            else
            {
                m_rootComponent.HandleTouch(touchPos);
            }            
        }
        
        //render
        SpriteBatch renderer = GraphicsManager.GetInstance().GetRenderer();
        renderer.begin();
        m_rootComponent.Render(renderer);
        renderer.end();
    }
 
}
